<?php

/**
 * BetlogModel.php 注释
 * 
 * @package 
 * @subpackage 
 * 
 */
class BetlogModel{
	/**
	 * 数据库对象
	 *
	 * @var Mysql
	 */
	private $db;
	
	/**
	 * 构造方法
	 *
	 * @param Mysql $db
	 */
	public function __construct($db)
	{
		$this->db = $db;
	}
	
	/**
	 * 添加
	 */
	public function add( array $value )
	{
		$v = array();
//		$v["id"] = (int)$value["id"];
		$v["uid"] = (int)$value["uid"];
		$v["bet"] = (int)$value["bet"];
		$v["beilv"] = (int)$value["beilv"];
		$v["usercash"] = (int)$value["usercash"];
		$v["beforeminpool"] = (int)$value["beforeminpool"];
		$v["afterminpool"] = (int)$value["afterminpool"];
		$v["beforemaxpool"] = (int)$value["beforemaxpool"];
		$v["aftermaxpool"] = (int)$value["aftermaxpool"];
		$v["win"] = (int)$value["win"];
		$v["ip"] = (int)$value["ip"];
		$v["bettime"] = $value["bettime"];
		
		$this->db->insert("betlog", $v);
	}
	
	/**
	 * 修改
	 */
	public function change($id, array $value)
	{
		$v = array();
		isset($value["uid"]) && $v["uid"] = (int)$value["uid"];
		isset($value["bet"]) && $v["bet"] = (int)$value["bet"];
		isset($value["beilv"]) && $v["beilv"] = (int)$value["beilv"];
		isset($value["usercash"]) && $v["usercash"] = (int)$value["usercash"];
		isset($value["beforeminpool"]) && $v["beforeminpool"] = (int)$value["beforeminpool"];
		isset($value["afterminpool"]) && $v["afterminpool"] = (int)$value["afterminpool"];
		isset($value["beforemaxpool"]) && $v["beforemaxpool"] = (int)$value["beforemaxpool"];
		isset($value["aftermaxpool"]) && $v["aftermaxpool"] = (int)$value["aftermaxpool"];
		isset($value["win"]) && $v["win"] = (int)$value["win"];
		isset($value["ip"]) && $v["ip"] = (int)$value["ip"];
		isset($value["bettime"]) && $v["bettime"] = $value["bettime"];

		$this->db->update("betlog", $v, 'id=' . (int)$id);
	}
	
	/**
	 * 删除
	 */
	public function del($id)
	{
		$this->db->delete("betlog", 'id=' .  (int)$id);
	}
	
	/**
	 * 读取列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function fetchAll($nowp, $per_page)
	{
		$where = '';		
		$totalNum = $this->db->fetchOne ('select count(*) from betlog ' . $where);
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select * from betlog' . $where .' order by id desc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	/**
	 * 显示
	 */
	public function show($id)
	{
		return $this->db->fetchRow("select * from `betlog` where id=\"?\"", array($id));
	}
}